package example.demo.juc.queue;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;

public class TestQueue {
    //SynchronousQueue跟Exchanger作用很像
    static SynchronousQueue<String> queue = new SynchronousQueue<>();

    public static void main(String[] args) throws Exception {
        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "===start");
                queue.put("wahaha");
                System.out.println(Thread.currentThread().getName() + "===end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println(Thread.currentThread().getName() + "===start");
                TimeUnit.SECONDS.sleep(3);
                System.out.println(queue.take());
                System.out.println(Thread.currentThread().getName() + "===end");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
